package pl.poznan.put.cs.idss.students.kingcart.ui;

import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

/**
 * 
 * Narzędzie do zaznaczania obszaru na Canvasie
 */
public class AreaSelection {

	private MainCanvas canvas;
	private Rectangle selection;

	private Point anchor;

	public AreaSelection(MainCanvas canvas) {
		this.canvas = canvas;
	}

	public void initSelection(Point point) {
		anchor = point;
		selection = new Rectangle(anchor);
	}

	public void select(Point point) {
		selection.setBounds((int) Math.min(anchor.x, point.getX()),
				(int) Math.min(anchor.y, point.getY()),
				(int) Math.abs(point.getX() - anchor.x),
				(int) Math.abs(point.getY() - anchor.y));
		canvas.repaint();

	}

	public void unselect() {
		selection = null;
		canvas.repaint();
	}

	public Rectangle getSelection() {
		return selection;
	}

	@SuppressWarnings("size of parentImage is sometimes wrong")
	private BufferedImage getSubImage(Image image) {
		if (image != null && selection != null)
			return ((BufferedImage) image).getSubimage(selection.x,
					selection.y, selection.width, selection.height);
		return null;
	}
}
